﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
//using FluentDateTime;

namespace Yglab.Models
{

	 public class NewInitializer : IDatabaseInitializer<YgContext>
	 {


		  #region IDatabaseInitializer<YgContext> Members

		  void IDatabaseInitializer<YgContext>.InitializeDatabase( YgContext context ) {
				if( context.Database.Exists() ) {

					 try {
						  if( !context.Database.CompatibleWithModel( true ) ) {
								context.Database.Delete();
								context.Database.Create();
								Seed( context );
						  }
					 }
					 catch( Exception e ) {
						  context.Database.Delete();
						  context.Database.Create();
						  Seed( context );

					 }

				} else {
					 context.Database.Create();
					 Seed( context );
				}


				context.SaveChanges();


		  }

		  #endregion


		  //public void InitializeDatabase(YgContext ygconn)
		  protected void Seed( YgContext ygconn ) {
				//truncateTables(ygconn);

				MySettings nesettings = new MySettings { DayNo = 0 };
				ygconn.MySettings.Add( nesettings );

				User admin = new User { Email = "admin@demir.com" , Password = "1234" , Name = "Admin" };
				ygconn.Users.Add( admin );
				ygconn.SaveChanges();

				Science science1 = new Science { Name = "Bilim Dalı 1" };
				Science science2 = new Science { Name = "Bilim Dalı 2" };
				Science science3 = new Science { Name = "Bilim Dalı 3" };
				Science science4 = new Science { Name = "Bilim Dalı 4" };
				ygconn.Sciences.Add( science1 );
				ygconn.Sciences.Add( science2 );
				ygconn.Sciences.Add( science3 );
				ygconn.Sciences.Add( science4 );

				Subject subject1 = new Subject { Name = "Araştırma Konusu 1" };
				Subject subject2 = new Subject { Name = "Araştırma Konusu 2" };
				Subject subject3 = new Subject { Name = "Araştırma Konusu 3" };
				Subject subject4 = new Subject { Name = "Araştırma Konusu 4" };
				Subject subject5 = new Subject { Name = "Araştırma Konusu 5" };
				Subject subject6 = new Subject { Name = "Araştırma Konusu 6" };
				ygconn.Subjects.Add( subject1 );
				ygconn.Subjects.Add( subject2 );
				ygconn.Subjects.Add( subject3 );
				ygconn.Subjects.Add( subject4 );
				ygconn.Subjects.Add( subject5 );
				ygconn.Subjects.Add( subject6 );

				Journal journal1 = new Journal { Name = "dergi 1" , PathName = "dergi-1" };
				Journal journal2 = new Journal { Name = "dergi 2" , PathName = "dergi-2" };
				ygconn.Journals.Add( journal1 );
				ygconn.Journals.Add( journal2 );
				ygconn.SaveChanges();

				// burda notification kısmını da ekleyeceğiz. 
				// InvitationToEditor InvEditor1 = new InvitationToEditor { Email = "user1@demir.com", Journal = journal1, Actepted = 0, LastAcceptTime = DateTime.Now + 1.Weeks() };

				User user1 = new User { Email = "user1@demir.com" , Password = "1234" , Name = "İsim1" , Surname = "Soyisim1" };
				ygconn.Users.Add( user1 );
				User user2 = new User { Email = "user2@demir.com" , Password = "1234" , Name = "İsim2" , Surname = "Soyisim2" };
				ygconn.Users.Add( user2 );
				User user3 = new User { Email = "user3@demir.com" , Password = "1234" , Name = "İsim3" , Surname = "Soyisim3" };
				ygconn.Users.Add( user3 );
				User user4 = new User { Email = "user4@demir.com" , Password = "1234" , Name = "İsim4" , Surname = "Soyisim4" };
				ygconn.Users.Add( user4 );
				User user5 = new User { Email = "user5@demir.com" , Password = "1234" , Name = "İsim5" , Surname = "Soyisim5" };
				ygconn.Users.Add( user5 );
				ygconn.SaveChanges();

		  }




	 }




    //public class TruncateAndSeedInitializer : IDatabaseInitializer<YgContext>
    public class TruncateAndSeedInitializer : DropCreateDatabaseIfModelChanges<YgContext>
    {
        //public void InitializeDatabase(YgContext ygconn)
        protected override void Seed(YgContext ygconn)
        {
            //truncateTables(ygconn);

            MySettings nesettings = new MySettings { DayNo = 0 };
            ygconn.MySettings.Add(nesettings);

            User admin = new User { Email = "admin@demir.com", Password = "1234", Name="Admin" };
            ygconn.Users.Add(admin);
            ygconn.SaveChanges();

            Science science1 = new Science { Name = "Bilim Dalı 1" };
            Science science2 = new Science { Name = "Bilim Dalı 2" };
            Science science3 = new Science { Name = "Bilim Dalı 3" };
            Science science4 = new Science { Name = "Bilim Dalı 4" };
            ygconn.Sciences.Add(science1);
            ygconn.Sciences.Add(science2);
            ygconn.Sciences.Add(science3);
            ygconn.Sciences.Add(science4);

            Subject subject1 = new Subject { Name = "Araştırma Konusu 1" };
            Subject subject2 = new Subject { Name = "Araştırma Konusu 2" };
            Subject subject3 = new Subject { Name = "Araştırma Konusu 3" };
            Subject subject4 = new Subject { Name = "Araştırma Konusu 4" };
            Subject subject5 = new Subject { Name = "Araştırma Konusu 5" };
            Subject subject6 = new Subject { Name = "Araştırma Konusu 6" };
            ygconn.Subjects.Add(subject1);
            ygconn.Subjects.Add(subject2);
            ygconn.Subjects.Add(subject3);
            ygconn.Subjects.Add(subject4);
            ygconn.Subjects.Add(subject5);
            ygconn.Subjects.Add(subject6);

            Journal journal1 = new Journal { Name = "dergi 1", PathName = "dergi-1" };
            Journal journal2 = new Journal { Name = "dergi 2", PathName = "dergi-2" };
            ygconn.Journals.Add(journal1);
            ygconn.Journals.Add(journal2);
            ygconn.SaveChanges();

            // burda notification kısmını da ekleyeceğiz. 
            // InvitationToEditor InvEditor1 = new InvitationToEditor { Email = "user1@demir.com", Journal = journal1, Actepted = 0, LastAcceptTime = DateTime.Now + 1.Weeks() };

            User user1 = new User { Email = "user1@demir.com", Password = "1234", Name = "İsim1", Surname = "Soyisim1" };
            ygconn.Users.Add(user1);
            User user2 = new User { Email = "user2@demir.com", Password = "1234", Name = "İsim2", Surname = "Soyisim2" };
            ygconn.Users.Add(user2);
            User user3 = new User { Email = "user3@demir.com", Password = "1234", Name = "İsim3", Surname = "Soyisim3" };
            ygconn.Users.Add(user3);
            User user4 = new User { Email = "user4@demir.com", Password = "1234", Name = "İsim4", Surname = "Soyisim4" };
            ygconn.Users.Add(user4);
            User user5 = new User { Email = "user5@demir.com", Password = "1234", Name = "İsim5", Surname = "Soyisim5" };
            ygconn.Users.Add(user5);
            ygconn.SaveChanges();

        }

        private void truncateTables(YgContext context)
        {
            if (context.Database.Exists())
            {
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("USE odev EXEC sp_MSforeachtable 'DELETE FROM ?'");
                context.Database.ExecuteSqlCommand("USE odev alter table Users add constraint AK_Users unique (Email)");
                context.SaveChanges();
            }
        }
    }
}